package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.FollowUpListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author ZQM
 * @date 2023/10/7 11:45
 */
@RestController
@Slf4j
@RequestMapping("/v1/followupList")
public class FollowUpListController extends BaseController {

    @Autowired
    private FollowUpListService followUpListService;

    /**
     * 随访列表新增
     */
    @PostMapping("/add")
    public JsonObj add() {
        return followUpListService.add(getPageData());
    }

    /**
     * 随访列表修改
     */
    @PostMapping("/edit")
    public JsonObj edit() {
        return followUpListService.edit(getPageData());
    }

    /**
     * 随访列表查询（单个）
     */
    @GetMapping("/selById")
    public JsonObj selById() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return followUpListService.selById(id);
    }

    /**
     * 随访列表查询（列表）
     */
    @PostMapping("/selList")
    public JsonObj selList() {
        return followUpListService.selList(getPageData());
    }

    /**
     * 随访列表删除
     */
    @GetMapping("/del")
    public JsonObj del() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return followUpListService.del(id);
    }

    /**
     * 导出
     */
    @PostMapping("queryAllExportData")
    public void queryAllExportData(HttpServletResponse response) {
        PageData pageData = getPageData();
        // 查询出可以用来导出的数据源
        JsonObj jsonObj = followUpListService.queryAllExportData(pageData);
        List<LinkedHashMap<String, Object>> dataList = (List<LinkedHashMap<String, Object>>) jsonObj
                .getData()
                .get("data");

        // 组装表头
        List<String> headList = Arrays.asList(
                "省份",
                "城市",
                "区县",
                "乡镇",
                "村",
                "机构名称",
                "调查对象ID号",
                "姓名",
                "性别",
                "生日",
                "民族",
                "身份证号",
                "地址",
                "联系电话",
                "筛查类型",
                "随访次数",
                "内镜号",
                "是否无痛",
                "染色类别",
                "是否活检",
                "内镜诊疗日期",
                "Hp",
                "Hp的检测方法",
                "图片数量",
                "内镜诊断",
                "内镜诊断医师",
                "内镜审核医师",
                "病理号",
                "病理报告日期",
                "食管病理诊断代码",
                "贲门病理诊断代码",
                "胃病理诊断代码",
                "十二指肠病理诊断代码",
                "病理诊断医师",
                "病理审核医师",
                "病理诊断");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "随访正式库导出", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
